var Flocking = function() {
		this.boids = [];
		this.rsize;
		this.tx;
		this.ty;
		this.targetx = [];
		this.targety = [];

		this.angle = [];

	}

Flocking.prototype.numPoints = 2;

Flocking.prototype.init = function(w, h, numTrails) {
	for (var i = 0; i < numTrails; i++) {
		var x = rnd(w);
		var y = rnd(h);

		var boid = new characterObj(x, y, 0, 0, 0.3, 0.4, i % 2 * 2 - 1, 0.8);

		this.boids[i] = boid; //这是一个数组
		this.targetx[i] = w / 2;
		this.targety[i] = h / 2;

		var beta = Math.atan2(y, x) + Math.PI * 1.5;
		this.angle[i] = beta;
	}

	this.tx = w / 2;
	this.ty = h / 2;

	this.rsize = w;

}

Flocking.prototype.target = function(tx, ty) {
	for (var i = 0; i < this.boids.length; i++) {
		this.targetx[i] = tx;
		this.targety[i] = ty;
	}

	this.tx = tx;
	this.ty = ty;

}

Flocking.prototype.updata = function(time) { //time值影响苍蝇转圈的大小

	var N = this.boids.length;
	for (var i = 0; i < N; i++) {

		var p0 = this.boids[i];

		var dx = p0.x - this.targetx[i];
		var dy = p0.y - this.targety[i];
		var ds = Math.sqrt(dx * dx + dy * dy);

		if (ds < this.rsize * 0.04) {
			var a = time * (rnd(0.001) + 0.003) * p0.d * 5000;
			this.targetx[i] = this.tx + this.rsize * 0.1 * Math.cos(a); //数值影响无头苍蝇的频率
			this.targety[i] = this.ty + this.rsize * 0.1 * Math.sin(a);
		}

		var ddx = -dx / ds;
		var ddy = -dy / ds;
		p0.ax = 0.6; //控制有多快的冲向中心
		p0.ay = 0.5;
		p0.vx += ddx * p0.ax; // - ddy*(rnd(2.0) - 1);
		p0.vy += ddy * p0.ay; // + ddx*(rnd(2.0) - 1);
		
		if ((ds < this.rsize * 0.01)) {
			p0.x += p0.vx * 0.1; //控制速度
			p0.y += p0.vy * 0.1;
		} else if ((ds < this.rsize * 0.05)) {
			p0.x += p0.vx * 0.05; //控制速度
			p0.y += p0.vy * 0.05;
		} else {
			p0.x += p0.vx * p0.spd; //控制速度
			p0.y += p0.vy * p0.spd;
		}

		//以下控制身体旋转
		var beta = Math.atan2(p0.vy, p0.vx) + Math.PI * 1.5;
		this.angle[i] = lerpAngle(beta, this.angle[i], 0.6);

		// draw the fish
		p0.draw(this.angle[i]);


		p0.vx *= p0.f;
		p0.vy *= p0.f;

	}
}